import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/goods/BuyGoods.dart';

class MyCollectGoodsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyCollectGoodsState();
  }
}

class MyCollectGoodsState extends State<MyCollectGoodsPage>
    with WidgetsBindingObserver {
  final SlidableController slidableController = SlidableController();

  EasyRefreshController _controller = new EasyRefreshController();
  List<bool> bools = new List();
  int currentStatus = 0;
  bool isSelectAll = false;
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      if (!ModalRoute.of(context).isCurrent) {
        return;
      }
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);

    bools..add(false)..add(false)..add(false)..add(false)..add(false);
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "我的收藏",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.2,
        iconTheme: new IconThemeData(color: black_99),
        backgroundColor: white,
        brightness: Brightness.light,
        centerTitle: true,
      ),
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        child: Column(
          children: <Widget>[
            Expanded(
              child: new EasyRefresh(
                controller: _controller,
                header: MaterialHeader(),
                footer: MaterialFooter(),
                enableControlFinishRefresh: true,
                child: ListView.builder(
                  itemCount: bools.length,
                  itemBuilder: (BuildContext context, int index) {
                    return renderView(context, index);
                  },
                ),
                onRefresh: () {},
              ),
            ),
          ],
        ),
        value: SystemUiOverlayStyle.dark,
      ),
    );
  }

  Widget renderView(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_15),
          ScreenUtil.getInstance().setHeight(DP_15),
          ScreenUtil.getInstance().setHeight(DP_15),
          0),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: Slidable(
        actionPane: SlidableStrechActionPane(),
        controller: slidableController,
        actionExtentRatio: 0.25,
        secondaryActions: <Widget>[
          IconSlideAction(
            caption: '删除',
            color: mainColor,
            icon: Icons.delete,
            closeOnTap: true,
            onTap: () {},
          ),
        ],
        child: Padding(
          padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
          child: new Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: CachedNetworkImage(
                imageUrl:
                    'https://img14.360buyimg.com/n0/jfs/t1/42163/17/16172/246472/5d8dd42cE12fa785a/efbd7ce9dbaac1c1.jpg',
                    width: ScreenUtil.getInstance().setHeight(DP_120),
                    fit: BoxFit.fill,
                    height: ScreenUtil.getInstance().setHeight(DP_120)),
              ),
              Container(
                width: ScreenUtil.getInstance().setHeight(DP_15),
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('【推广试饮 首瓶11元】贵州茅台镇酱香型白酒53度 高粱纯粮食高度原浆怀郎老酒 1瓶试饮装',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black87)),
                    Container(
                      height: ScreenUtil.getInstance().setHeight(DP_10),
                    ),
                    Text(MONEY + '269.00',
                        maxLines: 1,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                            color: mainColor))
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  checkAll() {
    for (int i = 0; i < bools.length; i++) {
      bools[i] = isSelectAll;
    }
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //销毁观察者
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
